const audio = require("../models/audio")
const fs = require('fs')

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, async function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
            console.log('ee')
            let audio_result = await audio.find({
                UUID: fields.UUID,
                pointId: fields.pointId,
                demoId: fields.demoId
            })
            console.log(audio_result)
            var read_file = function(au) {
                return new Promise((resolve, reject) => {
                    fs.readFile(au[au.length-1].path, function(error, data) {
                        // console.log(data.toString())
                        if(error) {
                          console.log('读取文件失败')
                          resolve({ 'state': false, 'result': '读取文件失败' })
                        } else {
                            console.log(data)
                          resolve({ 'state': true, 'result':data.toString() });
                        }
                    })
                })
            }

            let returndata = await read_file(audio_result)
            res.json(returndata)
        }

    });

});

module.exports = router;